<?php
namespace Home\Model;
use Think\Model;
use Common\Common\Classes\Message;

class TeacherService extends Model{
    private $tea_sql;
    public function __construct(){
        $this->tea_sql = D("Teacher");
    }
    //---------------------------------老师登陆---------------------------------
    public function check_login(){
        $where["tea_email"] = I("post.number");           //组织条件语句
        $where["tea_password_hash"] = md5(I("post.password"));
        if($this->tea_sql->create($where,4)) {
            $result = $this->tea_sql->where($where)->find();
            if ($result != "") {
                $_SESSION["name"] = $result['tea_name'];
                $_SESSION['isTeacher'] = "true";
                $_SESSION['number'] = $result["tea_email"];
                $_SESSION["id"] = $result["tea_id"];
                $data = new Message(1, "登陆成功,可以转到主页!", "");
            } else {
                $data = new Message(0, "账号或密码错误!", "asds");
            }
        }
        else{
            $data = new Message(0, $this->tea_sql->getError(), "asd");
        }
        return $data;
    }
    //---------------------------------老师注册---------------------------------
    public function register(){
        if($this->tea_sql->create($_POST,1)){
            $_POST["email"] = $_POST["tea_email"];
            $stu_sql = new StudentService();
            if($this->check_email()->status == 1 && $stu_sql->check_email()->status == 1 && $this->check_number()->status == 1){
                unset($_POST["email"]);
                $this->tea_sql->add($_POST);
                $data = new Message(1,"教师注册成功!","");
            }
            else{
                $data = new Message(0,"注册信息有错误!","");
            }
        }
        else{
            $data = new Message(0,$this->tea_sql->getError(),"");
        }
        return $data;
    }
    //---------------------------------检测老师邮箱是否存在---------------------------------
    public function check_email(){        //检测老师邮箱是否存在
        $where["tea_email"] = I("post.email");
        $result = $this->tea_sql->where($where)->find();
        if ($result != "") {
            $data = new Message(0, "该邮箱已经被注册!", "");
        } else {
            $data = new Message(1, "该邮箱可以使用!", "");
        }
        return  $data;
    }
    //---------------------------------检测老师工号是否存在---------------------------------
    public function check_number(){
        $where["tea_number"] = I("post.tea_number");
        $where["college_col_id"] = I("post.college_col_id");
        if($this->tea_sql->create($where,5)) {
            if($this->tea_sql->where($where)->find()!=""){
                $data = new Message(0,"当前学校下教职证号已存在!","");
            }
            else{
                $data = new Message(1,"教职证号可以注册!","");
            }
        }
        else{
            $data = new Message(0,$this->tea_sql->getError(),"");
        }
        return $data;
    }
    //---------------------------------获得老师的信息---------------------------------
    public function getInfo($field){
        $info = $this->tea_sql->relation("college")->field($field)->find($_SESSION["id"]);
        $info["state"] = "教师";
        return $info;
    }
    //---------------------------------更新老师的信息---------------------------------
    public function updateTeacher(){
        if($this->tea_sql->create($_POST,6)){
            $where["tea_id"] = $_SESSION["id"];
            $this->tea_sql->where($where)->save($_POST);
            $_SESSION["name"] = $_POST["tea_name"];
            $data = new Message(1,"信息修改成功!","");
        }
        else{
            $data = new Message(0,$this->tea_sql->getError(),"");
        }
        return $data;
    }
    //---------------------------------修改老师密码---------------------------------
    public function updatePWD(){
        $where["tea_id"] = $_SESSION["id"];
        $where["tea_password_hash"] = $_POST["old_pwd"];
        if($this->tea_sql->where($where)->find()!=""){
            $update["tea_password_hash"]=$_POST["new_pwd"];
            if($this->tea_sql->create($update,3)){
                $this->tea_sql->where($where)->save($update);
                $data = new Message(1,"密码修改成功!","");
            }
            else{
                $data = new Message(0,$this->tea_sql->getError(),"");
            }
        }
        else{
            $data = new Message(0,"密码认证错误,无法修改!","");
        }
        return $data;
    }
    public function __destruct(){}
}